#pragma once

#include<iostream>
#include<pthread.h>

class Mutex
{
public:
    Mutex() //初始化
    {
        pthread_mutex_init(&_lock,nullptr);
    }
    pthread_mutex_t* Get()
    {
        return &_lock; //完全不推荐使用返回值拷贝；
    }

    void Lock() //上锁
    {
        pthread_mutex_lock(&_lock);
    }
    void UnLock() //解锁
    {
        pthread_mutex_unlock(&_lock);
    }

    ~Mutex()//销毁
    {
        pthread_mutex_destroy(&_lock);
    }

private:
    pthread_mutex_t _lock;
    
};

class LockGuard
{
public:
    LockGuard(Mutex* mutex):_mutexp(mutex){
        _mutexp->Lock();
    }

    ~LockGuard(){
        _mutexp->UnLock();
    }

private:

    Mutex* _mutexp;

};

